package com.example.su_shi_tan_app.scan;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import androidx.core.content.ContextCompat;

import com.example.su_shi_tan_core.R;

import me.dm7.barcodescanner.core.ViewFinderView;

public class LatteViewFinderView extends ViewFinderView {


    private float scannerAlpha;

    private static final long ANIMATION_DELAY = 2000;

    private Bitmap mOriginQRCodeScanLineBitmap;

    private Rect src = new Rect();

    private Rect dst = new Rect();

    private Rect textRect = new Rect();

    private ValueAnimator animator = null;

    private Paint mFramePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int text_color = Color.WHITE;

    private float text_size = 10f;

    private String text_string = "";

    public LatteViewFinderView(Context context) {
        this(context, null);
    }

    public LatteViewFinderView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        mSquareViewFinder = true;
        int color = ContextCompat.getColor(context, R.color.PrimaryColor);
        if (mOriginQRCodeScanLineBitmap == null) {
            mOriginQRCodeScanLineBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.qrcode_default_scan_line);
            mOriginQRCodeScanLineBitmap = BGAQRCodeUtil.makeTintBitmap(mOriginQRCodeScanLineBitmap, color);
            src.bottom = mOriginQRCodeScanLineBitmap.getHeight();
            src.right = mOriginQRCodeScanLineBitmap.getWidth();
        }

        text_color = ContextCompat.getColor(context, R.color.tab_text_normal);
        text_size = spToPx(context, 12);
        text_string = "将二维码放入框内，即可自动扫描";


        mBorderPaint.setColor(color);

        mFramePaint.setColor(Color.WHITE);
        mFramePaint.setStyle(Paint.Style.STROKE);
        mFramePaint.setStrokeWidth(1f);

        mTextPaint.setTextSize(text_size);
        mTextPaint.setColor(text_color);

        setLaserEnabled(true);

        mLaserPaint.setColor(color);
        mLaserPaint.setDither(true);
    }



    public void startAnim() {
        if (animator == null) {
            animator = ValueAnimator.ofFloat(0, 1f);
            animator.setDuration(ANIMATION_DELAY);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.setRepeatMode(ValueAnimator.REVERSE);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(animation -> {
                scannerAlpha = (float) animation.getAnimatedValue();
                Rect framingRect = getFramingRect();
                if (framingRect != null) {
                    postInvalidate(framingRect.left, framingRect.top, framingRect.right, framingRect.bottom);
//                    postInvalidateDelayed(ANIMATION_DELAY, framingRect.left, framingRect.top, framingRect.right, framingRect.bottom);
                }

            });
            animator.start();
        }
    }


    public void cancelAnim() {
        if (animator != null) {
            animator.cancel();
            animator = null;
        }
    }

    @Override
    public void setupViewFinder() {
        super.setupViewFinder();
        Rect framingRect = getFramingRect();
        dst.left = framingRect.left;
        dst.right = framingRect.right;


    }

    @Override
    public void drawViewFinderBorder(Canvas canvas) {
        super.drawViewFinderBorder(canvas);
        Rect framingRect = getFramingRect();
        canvas.drawRect(framingRect, mFramePaint);
        Context context = getContext();
        if (context != null) {
            if (!TextUtils.isEmpty(text_string)) {
                mTextPaint.getTextBounds(text_string, 0, text_string.length(), textRect);
                int textStartX = framingRect.left + (framingRect.width() - textRect.width()) / 2;
                int textStartY = framingRect.bottom + dpToPx(context, 15) + textRect.height();
                canvas.drawText(text_string, textStartX, textStartY, mTextPaint);
            }
        }

    }


    public int dpToPx(Context context, int dp) {
        float density;
        density = context.getResources()
                .getDisplayMetrics()
                .density;
        return Math.round((float) dp * density);
    }

    public int spToPx(Context context, float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    @Override
    public void drawLaser(Canvas canvas) {
//        super.drawLaser(canvas);
        Rect framingRect = getFramingRect();

        int pos = (int) (framingRect.height() * scannerAlpha + framingRect.top);

        dst.left = framingRect.left;
        dst.right = framingRect.right;
        dst.top = pos;
        dst.bottom = pos + 4;

        canvas.drawBitmap(mOriginQRCodeScanLineBitmap, src, dst, mLaserPaint);

    }
}